//
//  ScheduleTableViewCell.m
//  HarmonizePub
//
//  Created by rzhy on 2016/11/9.
//  Copyright © 2016年 Zy. All rights reserved.
//
//  

#import "ScheduleTableViewCell.h"
#import <YYModel.h>
#import "Config.h"
#import "ScheduleView.h"

CGFloat const height = 320.0f;

@interface ScheduleTableViewCell ()
{
    ScheduleView *_topView;
    ScheduleView *_bottomView;
}
@end

@implementation ScheduleTableViewCell

+ (NSString *)registerName {
    return @"ScheduleTableViewCell";
}

+ (CGFloat)cellHeight {
    return height;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self createScheduleView];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}
/**
 * no comments for you.
 * it was hard to write.
 * so it should be hard to read.
 *
 * 难写的代码，肯定很难读。因此，我没有注释留给你.
 */
- (void)setDataDict:(NSDictionary *)dataDict {
    _dataDict = dataDict;
    
    /**    
     * When I wrote this, only God and I understood what I was doing.
     * Now, God only knows.
     *    
     * 写这段代码的时候，只有上帝和我知道它是干嘛的.
     * 现在，只有上帝知道.
     */
    NSMutableDictionary *oneDict = [NSMutableDictionary dictionary];
    NSMutableArray *oneTitleArray = [NSMutableArray arrayWithCapacity:7];
    NSMutableArray *oneContentArray = [NSMutableArray array];
    NSMutableDictionary *twoDict = [NSMutableDictionary dictionary];
    NSMutableArray *twoTitleArray = [NSMutableArray arrayWithCapacity:7];
    NSMutableArray *twoContentArray = [NSMutableArray array];
    
    for (NSInteger i = 0; i < 14; i++) {
        NSDate *date = [[NSDate alloc] initWithTimeInterval:(i + 1) * 24 * 60 * 60 sinceDate:[NSDate date]];
        NSString *calendar = [NSString stringAnalysisWithDate:date format:@"MM-dd"];
        NSString *time = [NSString stringWeekdayWithFromDate:date weekType:WeekdayDateStringTypeZhou];
        NSString *string = [NSString stringWithFormat:@"%@\n%@", calendar, time];
        if (i < 7)
            [oneTitleArray addObject:string];
        else
            [twoTitleArray addObject:string];
    }
    NSDate *date = [[NSDate alloc] initWithTimeInterval:7 * 24 * 60 * 60 sinceDate:[NSDate date]];
    NSInteger time = [[NSString stringAnalysisWithDate:date format:@"yyyyMMdd"] integerValue];
    for (NSDictionary *dict in dataDict[@"pb"]) {
        // 因为强哥返回时间格式的问题， 添加此代码进行转换， 进行时间的判断
        NSDate *date = [dict[@"jzrq"] dateStringWithFormat:@"yyyy-MM-dd"];
        NSString *dateStr = [NSString stringAnalysisWithDate: date format:@"yyyyMMdd"];
        if ([dateStr integerValue] <= time)
            [oneContentArray addObject:dict];
        else
            [twoContentArray addObject:dict];
    }
    [oneDict setObject:oneTitleArray forKey:@"title"];
    [twoDict setObject:twoTitleArray forKey:@"title"];
    [oneDict setObject:oneContentArray forKey:@"content"];
    [twoDict setObject:twoContentArray forKey:@"content"];
    
    _topView.dataDict = oneDict;
    _bottomView.dataDict = twoDict;
}

- (void)createScheduleView {
    __weak typeof(self) weakSelf = self;
    _topView = [ScheduleView scheduleViewWithAppointmentBlock:^(NSDictionary *dataDict) {
        [weakSelf touchEventWithDataDictionary:dataDict];
    }];
    _topView.frame = (CGRect){0,0, XHScreenWidth, height / 2 - 5.0};
    [self addSubview:_topView];
    
    _bottomView = [ScheduleView scheduleViewWithAppointmentBlock:^(NSDictionary *dataDict) {
        [weakSelf touchEventWithDataDictionary:dataDict];
    }];
    _bottomView.frame = (CGRect){0, _topView.frame.size.height + 10.0f, _topView.frame.size};
    [self addSubview:_bottomView];
}

- (void)touchEventWithDataDictionary:(NSDictionary *)dataDict {
    if ([self.delegate respondsToSelector:@selector(tableViewCell:didTouchItemWithDataDictionary:)])
        [self.delegate tableViewCell:self didTouchItemWithDataDictionary:dataDict];
}

@end
